package com.dhcc.sdc.cdss.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.core.framework.base.service.impl.BaseServiceImpl;
import com.dhcc.sdc.base.entity.SdcQcCdssLinkEntity;
import com.dhcc.sdc.base.service.ICdssStandardMatchService;
import com.dhcc.sdc.base.service.ISdcQcCdssLinkService;
import com.dhcc.sdc.busi.dao.SdcShowPremiseMapper;
import com.dhcc.sdc.busi.entity.SdcQcEntity;
import com.dhcc.sdc.busi.entity.SdcShowPremiseEntity;
import com.dhcc.sdc.busi.service.ISdcQcService;
import com.dhcc.sdc.cdss.dao.SdcCdssTreachildrenMapper;
import com.dhcc.sdc.cdss.dao.SdcCdssTreatMapper;
import com.dhcc.sdc.cdss.dto.CdssTreatChildrenDto;
import com.dhcc.sdc.cdss.entity.SdcCdssTreachildrenEntity;
import com.dhcc.sdc.cdss.entity.SdcCdssTreatEntity;
import com.dhcc.sdc.cdss.service.ISdcCdssTreatService;
import com.dhcc.sds.standard.entity.SdsStandSourceEntity;
import com.dhcc.sds.standard.entity.SdsStandSourceFieldEntity;
import com.dhcc.sds.standard.service.ISdsStandSourceFieldService;
import com.dhcc.sds.standard.service.ISdsStandSourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * 表数据服务层接口
 * 
 * @ClassName: SdcCdssTreatServiceImpl
 * @Description: TODO
 * @author zhangdc
 * @date 2022-06-23 15:46:45
 */
@Service("sdcCdssTreatService")
public class SdcCdssTreatServiceImpl extends BaseServiceImpl<SdcCdssTreatMapper, SdcCdssTreatEntity> implements ISdcCdssTreatService {

	@Resource
	private SdcCdssTreachildrenMapper cdssTreachildrenMapper;
	@Autowired
	private ISdcQcService sdcQcService;
	@Autowired
	private ISdsStandSourceService standSourceService;
	@Autowired
	private ISdsStandSourceFieldService sdsStandSourceFieldService;
	@Autowired
	private ICdssStandardMatchService cdssStandardMatchService;
	@Resource
	private SdcShowPremiseMapper sdcShowPremiseMapper;
	@Autowired
	private ISdcQcCdssLinkService sdcQcCdssLinkService;



	@Override
	public List<SdcCdssTreatEntity> page(Page<SdcCdssTreatEntity> page, Map<String, Object> map) {
		List<SdcCdssTreatEntity> page1 = this.baseMapper.page(page, map);
		for (SdcCdssTreatEntity cdssTreatEntity : page1){
			SdcQcCdssLinkEntity sdcQcCdssLinkEntity = sdcQcCdssLinkService.selectByNodeId(cdssTreatEntity.getNodeId());
			if (!StringUtils.isEmpty(sdcQcCdssLinkEntity)){
				SdcQcEntity sdcQcEntity = sdcQcService.selectById(sdcQcCdssLinkEntity.getQcItemId());
				if (!StringUtils.isEmpty(sdcQcEntity)){
					cdssTreatEntity.setQcDesc(sdcQcEntity.getQcDesc());
				}
			}
			if (!StringUtils.isEmpty(cdssTreatEntity.getDataSource()) && !StringUtils.isEmpty(cdssTreatEntity.getDataSourceItem())){
				/*SdsStandSourceEntity sourceEntity = standSourceService.selectById(cdssTreatEntity.getDataSource());
				SdsStandSourceFieldEntity sourceFieldEntity = sdsStandSourceFieldService.selectById(cdssTreatEntity.getDataSourceItem());
				if (!StringUtils.isEmpty(sourceEntity) && !StringUtils.isEmpty(sourceFieldEntity)){
					cdssTreatEntity.setReferenceTime(sourceEntity.getSourceDesc() + "-" + sourceFieldEntity.getFieldDesc());
				}*/
				String referenceTime = cdssStandardMatchService.handleReferenceTime(SdcCdssTreatEntity.class, cdssTreatEntity);
				cdssTreatEntity.setReferenceTime(referenceTime);
			}
		}
		return page1;
	}
	@Override
	public List<SdcCdssTreatEntity> list(Map<String, Object> map) {
		return this.baseMapper.list(map);
	}

	@Override
	public String handleJsonArrayData(JSONArray jsonArray, Long qcEntityId, List<String> cdssTreatNodeIds) {
		String res;
		int treatCount = 0;
		int treatChildrenCount = 0;

		List<SdcCdssTreatEntity> cdssTreatEntities = JSONObject.parseArray(jsonArray.toJSONString(), SdcCdssTreatEntity.class);
		for (SdcCdssTreatEntity cdssTreatEntity : cdssTreatEntities){
			//List<CdssTreatChildrenDto> cdssTreatChildrenDtos = cdssTreatEntity.getChildren();
			SdcCdssTreatEntity cdssTreatEntityNew = new SdcCdssTreatEntity();
			cdssTreatEntityNew.setQcentityId(qcEntityId);
			cdssTreatEntityNew.setItemId(cdssTreatEntity.getItemId());
			cdssTreatEntityNew.setNodeId(cdssTreatEntity.getNodeId());
			cdssTreatNodeIds.add(cdssTreatEntity.getNodeId());
			SdcCdssTreatEntity cdssTreatEntity1 = this.baseMapper.selectOne(cdssTreatEntityNew);
			if (StringUtils.isEmpty(cdssTreatEntity1)){
				//insert
				cdssTreatEntity.setQcentityId(qcEntityId);
				cdssTreatEntity.setIsCdss(1);
				boolean insert = cdssTreatEntity.insert();
				if (insert){
					treatCount++;
				}
			}else {
				//update
				cdssTreatEntity1.setQcentityId(qcEntityId);
				cdssTreatEntity1.setItemDesc(cdssTreatEntity.getItemDesc());
				cdssTreatEntity1.setItemName(cdssTreatEntity.getItemName());
				cdssTreatEntity1.setSourceFlag(cdssTreatEntity.getSourceFlag());
				cdssTreatEntity1.setNoCondition(cdssTreatEntity.getNoCondition());
				cdssTreatEntity1.setCureId(cdssTreatEntity.getCureId());
				boolean b = cdssTreatEntity1.updateAllColumnById();
				if (b){
					treatCount++;
				}
			}

			/*if (cdssTreatChildrenDtos != null && cdssTreatChildrenDtos.size() > 0){
				for (CdssTreatChildrenDto cdssTreatChildrenDto : cdssTreatChildrenDtos){
					SdcCdssTreachildrenEntity cdssTreachildrenEntityNew = new SdcCdssTreachildrenEntity();
					cdssTreachildrenEntityNew.setItemId(cdssTreatEntity.getItemId());
					cdssTreachildrenEntityNew.setChildrenId(cdssTreatChildrenDto.getItemId());
					SdcCdssTreachildrenEntity sdcCdssTreachildrenEntity = cdssTreachildrenMapper.selectOne(cdssTreachildrenEntityNew);
					if (StringUtils.isEmpty(sdcCdssTreachildrenEntity)){
						//insert
						cdssTreachildrenEntityNew.setChildrenName(cdssTreatChildrenDto.getTreatItemName());
						cdssTreachildrenEntityNew.setChildrenType(cdssTreatChildrenDto.getTreatItemType());
						cdssTreachildrenEntityNew.setSourceFlag(cdssTreatChildrenDto.getSourceFlag());
						boolean insert = cdssTreachildrenEntityNew.insert();
						if (insert){
							treatChildrenCount++;
						}
					}else {
						//update
						sdcCdssTreachildrenEntity.setChildrenName(cdssTreatChildrenDto.getTreatItemName());
						sdcCdssTreachildrenEntity.setChildrenType(cdssTreatChildrenDto.getTreatItemType());
						sdcCdssTreachildrenEntity.setSourceFlag(cdssTreatChildrenDto.getSourceFlag());
						boolean b = sdcCdssTreachildrenEntity.updateAllColumnById();
						if (b){
							treatChildrenCount++;
						}
					}
				}
			}*/
		}
		res = "新增/更新父表" + treatCount + "条;" + "新增/更新子表" + treatChildrenCount + "条";
		return res;
	}

	@Override
	public List<Map<String, Object>> sdcItemList(Map<String, Object> map) {
		return this.baseMapper.sdcItemList(map);
	}

	@Override
	public List<String> selectNodeIds(Long entityId) {
		return this.baseMapper.selectNodeIds(entityId);
	}

	@Override
	public void deleteByNodeIds(List<String> nodeIds) {
		this.baseMapper.deleteByNodeIds(nodeIds);
	}

	@Override
	public List<SdcShowPremiseEntity> selectByShowPremise(Map<String, Object> params) {
		List<SdcShowPremiseEntity> res = new ArrayList<>();

		String compare = (String) params.get("compare");
		String showPremise = "";
		if (!StringUtils.isEmpty(params.get("showPremise"))){
			try {
				showPremise = java.net.URLDecoder.decode((String) params.get("showPremise"), "UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			List<String> list = handleRuleExpressions(showPremise);
			if (Objects.equals("all", compare)){
				String s = list.get(0);
				String[] split = s.split("_");
				params.put("expCode", split[0]);
				res = sdcShowPremiseMapper.list(params);
			}else {
				res = sdcShowPremiseMapper.selectByExpressions(list);
			}
		}


		for (SdcShowPremiseEntity sdcShowPremiseEntity : res){
			String ruleCal = "";
			if (!StringUtils.isEmpty(sdcShowPremiseEntity.getDataSource1()) && !StringUtils.isEmpty(sdcShowPremiseEntity.getDataSourceItem1())){
				SdsStandSourceEntity sourceEntity1 = standSourceService.selectById(sdcShowPremiseEntity.getDataSource1());
				SdsStandSourceFieldEntity sourceFieldEntity1 = sdsStandSourceFieldService.selectById(sdcShowPremiseEntity.getDataSourceItem1());
				ruleCal = sourceEntity1.getSourceDesc() + ":" + sourceFieldEntity1.getFieldDesc();
			}
			if (!StringUtils.isEmpty(sdcShowPremiseEntity.getParam1())){
				ruleCal = ruleCal + "(" + sdcShowPremiseEntity.getParam1() + ")";
			}
			if (!StringUtils.isEmpty(sdcShowPremiseEntity.getOperator1())){
				ruleCal = ruleCal + sdcShowPremiseEntity.getOperator1();
			}
			if (!StringUtils.isEmpty(sdcShowPremiseEntity.getDataSource2()) && !StringUtils.isEmpty(sdcShowPremiseEntity.getDataSourceItem2())){
				SdsStandSourceEntity sourceEntity2 = standSourceService.selectById(sdcShowPremiseEntity.getDataSource2());
				SdsStandSourceFieldEntity sourceFieldEntity2 = sdsStandSourceFieldService.selectById(sdcShowPremiseEntity.getDataSourceItem2());
				ruleCal = ruleCal + sourceEntity2.getSourceDesc() + ":" + sourceFieldEntity2.getFieldDesc();
			}
			if (!StringUtils.isEmpty(sdcShowPremiseEntity.getParam2())){
				ruleCal = ruleCal + "(" + sdcShowPremiseEntity.getParam2() + ")";
			}
			if (!StringUtils.isEmpty(sdcShowPremiseEntity.getOperator2())){
				ruleCal = ruleCal + sdcShowPremiseEntity.getOperator2();
			}
			sdcShowPremiseEntity.setRuleCal(ruleCal);
		}
		return res;
	}

	private List<String> handleRuleExpressions(String ruleExpressions) {
		//小括号及其里面内容
		String p1 = "\\(.*?\\)";
		List<String> res = new ArrayList<>();
		Pattern p = Pattern.compile(p1);
		Matcher m = p.matcher(ruleExpressions);
		while (m.find()){
			String ruleExpression = m.group().substring(1, m.group().length() - 1);
			res.add(ruleExpression);
		}
		return res;
	}
}